<template>
  <n-layout has-sider class="admin-container">
    <transition name="slide-left">
      <n-layout-sider
        v-if="menuShow"
        collapse-mode="width"
        :collapsed-width="120"
        :width="255"
        bordered
        :class="['text-[#fff]', 'wp-layout']"
      >
        <div class="png-mask"></div>
        <div class="grent-mask"></div>
        <div
          class="w-full h-[70px] flex justify-center items-center bg-opacity-5 mb-[10px]"
          :class="[`bg-[#fff]`]"
        >
          <h1 class="text-[30px] cursor-pointer logo" @click="handleToHome">
            {{ 'MIN-题库' }}
          </h1>
        </div>
        <wp-menu
          class="my-menu"
          :items="menuItems"
          @menu-item-click="handleMenuChange"
        ></wp-menu>
      </n-layout-sider>
    </transition>
    <div
      :class="['bg-primaryColor']"
      class="left-menu-btn cursor-pointer bg-opacity-50"
      @click="() => changeMenuShow(!menuShow)"
      v-if="isSmall"
    >
      <svg-icon icon="zhedie2" size="20px" color="#fff"></svg-icon>
    </div>
    <n-layout-content class="main">
      <div
        class="mask"
        v-if="showMask"
        @click="() => changeMenuShow(false)"
      ></div>
      <div class="header">
        <n-breadcrumb>
          <span class="bread-start mr-[10px]"></span>
          <n-breadcrumb-item v-for="bread in breadPath" :key="bread.id">
            {{ bread.title }}
          </n-breadcrumb-item>
        </n-breadcrumb>
      </div>
      <div class="view">
        <router-view></router-view>
      </div>
    </n-layout-content>
  </n-layout>
</template>

<script setup lang="ts">
import useSettingsStore from '@/stores/modules/settings'
import useUserStore from '@/stores/modules/user'
import type { IndexObj } from '@/types/commom'
import { userMenuShow } from '@/hooks/useMenuShow'
import SvgIcon from '@/components/SvgIcon/index.vue'
import WpMenu from './components/WpMenu.vue'
import { filterAdminMenu, adminMenu } from './config'

const { isSmall, menuShow, showMask, changeMenuShow } = userMenuShow()
const settingsStore = useSettingsStore()
const userStore = useUserStore()
const router = useRouter()

const menuItems = ref<IndexObj[]>([])
const breadPath = ref<IndexObj[]>([])

// 根据用户权限的不同生成不同的左边侧边栏
const newMenu = computed(() => {
  if (userStore.permissions === undefined) {
    return []
  }
  return filterAdminMenu(userStore.permissions, adminMenu)
})

// 根据菜单数组和当前项名称，获取面包屑数组
const findRoutePath = (
  menu: IndexObj[],
  name: string,
  path: IndexObj[] = []
): IndexObj[] => {
  for (const item of menu) {
    if (item.name === name) {
      path.push(item)
      return path
    }
    if (item.children) {
      const childPath = findRoutePath(item.children, name, path.concat(item))
      if (childPath.length > 0) {
        return childPath
      }
    }
  }
  return []
}

// 菜单当前项发生改变
const handleMenuChange = (value: IndexObj) => {
  // 修改面包屑
  breadPath.value = findRoutePath(newMenu.value, value.name)
  showMask.value && changeMenuShow(false)
  router.push({
    name: value.name
  })
}

// 根据菜单项和名称，获取到目标菜单项
const findMenuItemByName = (
  menuItems: IndexObj[],
  name: string
): any | null => {
  for (let i = 0; i < menuItems.length; i++) {
    const menuItem = menuItems[i]

    // 如果当前菜单项的ID匹配目标ID，则返回当前菜单项
    if (menuItem.name === name) {
      return menuItem
    }

    // 如果当前菜单项有子菜单项，则递归查找子菜单项
    if (menuItem.children && menuItem.children.length > 0) {
      const foundMenuItem = findMenuItemByName(menuItem.children, name)

      // 如果在子菜单项中找到了目标ID，则返回子菜单项
      if (foundMenuItem) {
        return foundMenuItem
      }
    }
  }

  // 如果在当前菜单项及其子菜单项中未找到目标ID，则返回null
  return null
}

// 直接地址栏输入url，更新当前菜单项
// onBeforeMount(async () => {
//   if (userStore.permissions === undefined) {
//     await userStore.getPermissions()
//   }

//   menuItems.value = newMenu.value
//   // console.log(newMenu.value)

// })

// 去往用户管理页面
const handleToHome = () => {
  router.push({
    name: 'home'
  })
}

onMounted(async () => {
  if (!userStore.userInfo) {
    await userStore.getUserInfo()
  }
  if (userStore.permissions === undefined) {
    await userStore.getPermissions()
  }
  menuItems.value = newMenu.value
  settingsStore.setMenuActiveIndex(
    findMenuItemByName(newMenu.value, router.currentRoute.value.name as string)
  )
  breadPath.value = findRoutePath(
    newMenu.value,
    router.currentRoute.value.name as string
  )
})

watch(
  () => router.currentRoute.value.name,
  (newValue) => {
    settingsStore.setMenuActiveIndex(
      findMenuItemByName(newMenu.value, newValue as string)
    )
    breadPath.value = findRoutePath(newMenu.value, newValue as string)
  },
  {
    deep: true
  }
)
</script>

<style scoped lang="scss">
.admin-container {
  height: 100vh;
  background: $bg-light-2;
  font-size: 14px;

  .my-menu {
    height: calc(100vh - 80px);
  }

  // 倒影标题
  .logo {
    text-align: center;
    -webkit-box-reflect: below -4px -webkit-gradient(
        linear,
        left top,
        left bottom,
        ‘ from(transparent),
        color-stop(60%, transparent),
        color-stop(70%, rgba(255, 255, 255, 0)),
        to(rgba(255, 255, 255, 0.3))
      );
    background-image: -webkit-linear-gradient(60deg, #ffffff, #14a1ff);
    background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  // 渐变按钮
  :deep(.grent-btn) {
    outline: none;
    border: 0;
    background: -webkit-linear-gradient(-200deg, #006cfa, #1acbff);
    // transition: all 1s;
    &:hover {
      background: -webkit-linear-gradient(-200deg, #0059ce, #00afe2);
    }
    .n-button__border,
    .n-button__state-border {
      border: none;
    }
  }
}

// 侧边栏右滑移入，左滑移除
.slide-left-enter-active {
  animation: slide-left-enter 0.5s;
}
.slide-left-leave-active {
  animation: slide-left-leave 0.5s;
}
@keyframes slide-left-enter {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0);
  }
}
@keyframes slide-left-leave {
  0% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-100%);
  }
}

.main {
  position: relative;
  .header {
    transition: all 0.3s;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    border-bottom: 1px solid #e6e6e6;
    height: 70px;
    padding: 0 12px 0 0;
    background: $bg-light-1;
    display: flex;
    align-items: center;
    padding-left: 25px;
    .bread-start {
      border-left: 4px solid $primaryColor;
      margin-right: 10px;
      display: inline-block;
      height: 18px;
      position: relative;
      top: 4px;
      line-height: 0;
      box-shadow: 1px 1px 3px rgba(24, 144, 255, 0.6);
      border-radius: 3px;
    }
  }
  .view {
    overflow: auto;
    height: calc(100vh - 70px);
    padding: 10px;
    background: $bg-light-2;
  }
}

.left-menu-btn {
  position: fixed;
  z-index: 1;
  top: 3px;
  left: 0;
  width: 40px;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}

// 模糊遮罩
.mask {
  z-index: 10;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.wp-layout {
  background-color: transparent !important;
  position: relative;
  .png-mask {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    background: url('@/assets/images/11.jpg') no-repeat;
    background-size: cover;
    background-position: 75% 50%;
    z-index: -2;
    // opacity: 0.1;
  }

  .grent-mask {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    opacity: 0.92;
    background: #001529;
    background-image: linear-gradient(45deg, #000000, #061161);
    z-index: -1;
  }
}

@media (max-width: 767px) {
  .wp-layout {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 11;
  }

  .main {
    .header {
      height: 40px;
      padding-left: 45px;
    }

    .view {
      height: calc(100vh - 40px);
    }
  }
}
</style>
